package jjn.effective_java.chapter6.item37;

import java.util.EnumMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Jiang Jining
 * @since 2023-04-28 21:31
 */
public class EnumMapDemo {
    public static void main(String[] args) {
        EnumMap<Plant.LifeStyle, Set<Plant>> map = new EnumMap<>(Plant.LifeStyle.class);
        for (Plant.LifeStyle lifeStyle : Plant.LifeStyle.values()) {
            map.put(lifeStyle, new LinkedHashSet<>());
        }
        List<Plant> garden = List.of(new Plant("Sunflower", Plant.LifeStyle.ANNUAL), new Plant("Poppy", Plant.LifeStyle.ANNUAL), new Plant("Phlox", Plant.LifeStyle.ANNUAL), new Plant("Daisy", Plant.LifeStyle.PERENNIAL), new Plant("Bluestem", Plant.LifeStyle.PERENNIAL), new Plant("Rose", Plant.LifeStyle.BIENNIAL));
        for (Plant plant : garden) {
            map.get(plant.lifeStyle()).add(plant);
        }
        map.forEach((k, v) -> System.out.println("k = " + k + ", v=" + v));
        System.out.println();
        EnumMap<Plant.LifeStyle, Set<Plant>> enumMap = garden.stream().collect(Collectors.groupingBy(Plant::lifeStyle, () -> new EnumMap<>(Plant.LifeStyle.class), Collectors.toSet()));
        enumMap.forEach((k, v) -> System.out.println("k = " + k + ", v=" + v));
    }
}
